package com.tang.companyjob.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tang.common.api.UserFeignClient;
import com.tang.common.entity.companyJob.Company;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;

import com.tang.common.utils.JwtUtil;
import com.tang.common.utils.UpLoad;
import com.tang.companyjob.service.CompanyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;


    @ApiOperation("企业列表")
    @RequestMapping("/companyList")
    public Result companyList(String keywords, Integer currentPage, Integer pageSize, Integer hasAuth) {
        if (hasAuth == null) {
            hasAuth = 1;
        }
        List<Company> list = companyService.list(keywords, currentPage, pageSize, hasAuth);
        return Result.success(PageInfo.of(list));
    }

    @RequestMapping("/register/companyIsLive")
    public Result companyIsLive(String companyName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("company_name", companyName);
        Company company1 = companyService.getOne(queryWrapper);
        if (company1 != null) {
            if (company1.getHasAuth() == 0) {
                return Result.error(RecruitExceptionEnum.NOT_AUIDT);
            }
            if (company1.getHasAuth() == 2) {
                boolean b = companyService.removeById(company1.getCompanyId());
                if (b) {
                    return Result.error(RecruitExceptionEnum.COMPANY_REGISTER);
                }
            }
            if (company1.getHasAuth() == 1) {
                return Result.success(company1);
            }
        }
        return Result.error(RecruitExceptionEnum.COMPANY_REGISTER);
    }

    @ApiOperation("删除企业")
    @RequestMapping("/delCompany")
    public Result delCompany(Integer companyId) {
        int result = companyService.deleteByCompanyId(companyId);
        if (result != 0) {
            return Result.success("删除成功");
        } else {
            return Result.error(RecruitExceptionEnum.DELETE_FAILED);
        }
    }

    @ApiOperation("获得")
    @RequestMapping("/getCompany")
    public Result getCompany(Integer companyId) {
        Company company = companyService.getById(companyId);
        if (company != null) {
            return Result.success(company);
        } else {
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }

    @RequestMapping("/getOrginCompany")
    public Result getCompany(String companyName) {
        QueryWrapper<Company> qw = new QueryWrapper<>();
        qw.eq("delete_flag", 1);
        qw.eq("company_name", companyName);
        Company company = companyService.getOne(qw);
        if(company!=null){
            return Result.success(company);
        }
        return  Result.error(RecruitExceptionEnum.NO_DATA);
    }

    @ApiOperation("审核企业")
    @RequestMapping("/auditCompany")
    public Result auditCompany(Integer companyId, String res) {
        String result = companyService.auditCompany(companyId, res);
        if (result != null) {
            return Result.success(result);
        } else {
            return Result.error(RecruitExceptionEnum.AUDIT_FAILED);
        }
    }

    @RequestMapping("/companyCount")
    public Integer getCompanyCount() {
        return companyService.getCompanyCount();
    }

    @RequestMapping("/register/upload")
    public Result register(
            @RequestParam("companyName") String companyName,
            @RequestParam("industry") String industry,
            @RequestParam("uniformCreditCode") String uniformCreditCode,
            @RequestParam("legalPerson") String legalPerson,
            @RequestParam("buildTime") Date buildTime,
            @RequestParam("email") String email,
            @RequestParam("address") String address,
            @RequestParam("companyType") Integer companyType,
            @RequestParam("cityCode") String cityCode,
            @RequestParam("tel") String tel,
            @RequestParam("registeredCapital") String registeredCapital,
            @RequestParam("file") List<MultipartFile> file,
            HttpServletRequest request
    ) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("company_name", companyName);
        Company company1 = companyService.getOne(queryWrapper);
        if (company1 != null && company1.getHasAuth() == 2) {
            companyService.removeById(company1.getCompanyId());
        } else if (company1 != null && company1.getHasAuth() == 0) {
            return Result.error(RecruitExceptionEnum.NOT_AUIDT);
        }
        Company company = new Company();
        company.setCompanyName(companyName);
        company.setIndustry(industry);
        company.setUniformCreditCode(uniformCreditCode);
        company.setLegalPerson(legalPerson);
        company.setBuildTime(buildTime);
        company.setEmail(email);
        company.setAddress(address);
        company.setCompanyType(companyType);
        Integer city = new Integer(cityCode);
        company.setCityCode(city);
        company.setTel(tel);
        BigDecimal bigDecimal = new BigDecimal(registeredCapital);
        company.setRegisteredCapital(bigDecimal);
        String token = request.getHeader("TOKEN");
        String username = JwtUtil.get(token, "username");
        company.setUserName(username);
        if (!file.isEmpty()) {
            List<String> files = UpLoad.upload(file);
            if (!files.isEmpty()) {
                company.setBusinessLicenseUrl(files.get(0));
            }
        }
        Company register = companyService.register(company);
        if (register != null) {
            return Result.success(register);
        }
        return Result.error(RecruitExceptionEnum.HANDLE_FAILED);
    }

    @RequestMapping("/companyInfo")
    public Result companyInfo(String companyName) {
        QueryWrapper query = new QueryWrapper();
        query.eq("company_name", companyName);
        Company company = companyService.getOne(query);
        if (company != null) {
            return Result.success(company);
        } else {
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }

    @RequestMapping("/updateInfo")
    public Result updateInfo(
            String companyName,
            String logo,
            Integer scale,
            String tel,
            String email,
            String address,
            String officialWebsiteUrl,
            String financeList,
            String recruitUrl,
            String legalPersonInfo,
            String businessScope,
            String companyInfo, HttpServletRequest request
    ) {
        Company company = companyService.selectByCompanyName(companyName);
        if (company != null) {
            String token = request.getHeader("TOKEN");
            String username = JwtUtil.get(token, "username");
            if (username.equals(company.getUserName())) {
                company.setCompanyLogoUrl(logo);
                company.setCompanyScale(scale);
                company.setTel(tel);
                company.setEmail(email);
                company.setAddress(address);
                company.setOfficialWebsiteUrl(officialWebsiteUrl);
                company.setFinanceList(financeList);
                company.setRecruitUrl(recruitUrl);
                company.setLegalPersonInfo(legalPersonInfo);
                company.setBusinessScope(businessScope);
                company.setCompanyInfo(companyInfo);
                boolean b = companyService.updateById(company);
                if (b) {
                    return Result.success("修改成功");
                }
            }
        }
        return Result.error(RecruitExceptionEnum.UPDATE_FAILED);
    }

}
